﻿using Abp.Domain.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Core.Entities
{
    //交易账簿通道
    [Table("Trade_AccountBook_Channel")]
    [Serializable]
    public class TradeAccountBookChannel : Entity<string>
    {
        //帐簿Id
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long BookId { get; set; }

        /// <summary>
        /// 通道Id
        /// </summary>
        [Column(Order = 1)]
        [StringLength(maximumLength: 32)]
        public string ChannelId { get; set; }

        //申请时间
        public DateTime ApplyTime { get; set; }

        //付款时间
        public DateTime PayTime { get; set; }

        //付款费用
        [Range(0.00, double.MaxValue)]
        public decimal PayFee { get; set; }

        //付款金额
        [Range(0.01, double.MaxValue)]
        public decimal PayMoney { get; set; }

        //退款金额
        [Range(0.00, double.MaxValue)]
        public decimal RefundMoney { get; set; }

        //退款费用
        [Range(0.00, double.MaxValue)]
        public decimal RefundFee { get; set; }

        //交易余额
        [Range(0.00, double.MaxValue)]
        public decimal TradeBalanceMoney { get; set; }

        //帐簿余额
        [Range(0.00, double.MaxValue)]
        public decimal BookBalanceMoney { get; set; }

        //客户端Ip
        [Required]
        [StringLength(maximumLength: 255)]
        public string ClientIP { get; set; }

        //通道交易号
        [StringLength(maximumLength: 100)]
        public string ChannelTradeNo { get; set; }

        //对方账号
        [StringLength(maximumLength: 100)]
        public string OtherPartyAccount { get; set; }

        //对方账户Id
        [StringLength(maximumLength: 100)]
        public string OtherPartyAccountId { get; set; }

        //银行类型
        [StringLength(maximumLength: 100)]
        public string BankType { get; set; }

        //银行名称
        [StringLength(maximumLength: 100)]
        public string BankName { get; set; }

        //账本时间
        public DateTime BookTime { get; set; }

        //交易账簿
        public virtual TradeAccountBook TradeAccountBook { get; set; }       

        /// <summary>
        /// 计算余额
        /// </summary>
        public void CalcBalanceMoney()
        {
            this.BookBalanceMoney = this.PayMoney - this.PayFee - this.RefundMoney - this.RefundFee;
            this.TradeBalanceMoney = this.PayMoney - this.RefundMoney;
        }

        /// <summary>
        /// 交易账本通道添加退款金额
        /// </summary>
        /// <param name="book">账本</param>
        public void AddRefundMoney(TradeRefundBook refundBook)
        {            
            if (refundBook == null)
            {
                throw new ArgumentNullException(nameof(refundBook));
            }
            this.RefundFee += refundBook.RefundFee;
            this.RefundMoney += refundBook.RefundMoney;
            this.CalcBalanceMoney();
        }

    }
}
